package com.zjp.aop.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.github.javafaker.Faker;
import com.zjp.aop.domain.UserLog;
import com.zjp.aop.domain.Vpc;
import com.zjp.aop.service.LogRecordTestService;
import com.zjp.aop.service.LogService;
import io.github.dk900912.oplog.annotation.OperationLog;
import io.github.dk900912.oplog.model.BizCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Locale;

@RequestMapping("/log")
@RestController
public class LogController {


    @Autowired
    LogService logService;


    @Autowired
    LogRecordTestService logRecordTestService;


    @GetMapping("/a")
    @ResponseBody
    public String a() {
        return "OK";
    }


    @GetMapping("/test")
    @ResponseBody
    public String test() {
        UserLog userLog = new UserLog();
        userLog.setUserId(RandomUtil.randomLong(0, 99));
        userLog.setDataLogId(RandomUtil.randomString(36));
        userLog.setBizNo(RandomUtil.randomString(20));
        logService.test(userLog);
        return userLog.toString();
    }

    @GetMapping("/test1")
    @ResponseBody
    public String test1() {
        return logService.test1(RandomUtil.randomString(3), RandomUtil.randomString(9));
    }

    @GetMapping("/test2")
    @ResponseBody
    public String test2() {
        Faker faker = Faker.instance(Locale.CHINA);
        return logService.test2(DateUtil.now(), faker.name().name(), RandomUtil.randomInt(0, 999999));
    }


    @GetMapping("/test3")
    @ResponseBody
    public String test3() {
        Faker faker = Faker.instance(Locale.CHINA);
        UserLog userLog = new UserLog();
        userLog.setUserId(RandomUtil.randomLong(0, 99));
        userLog.setDataLogId(RandomUtil.randomString(36));
        userLog.setBizNo(RandomUtil.randomString(20));
        return logRecordTestService.test(userLog);
    }

    @OperationLog(
            bizCategory = BizCategory.UPDATE,
            bizTarget = "VPC",
            bizNo = "#vpc.id",
            diffSelector = "com.zjp.aop.service.VpcService#findVpcById(String)"
    )
    @PostMapping("vpc")
    public Vpc t2(@RequestBody Vpc vpc) {
        return vpc;
    }

}
